import BaseItemView from "../../cfw/mvc/BaseItemView";
import {MAX_STATE_NUM, EventName, PlayerStateID } from "../../public/Config";
import User from "../db/User";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import StateItemModel from "../player/model/StateItemModel";
import Player from "../player/Player";


const { ccclass, property } = cc._decorator;

@ccclass
export default class LobbyButtonView extends BaseItemView {


    @property(cc.Mask)
    mask: cc.Mask = null;

    @property(cc.Node)
    arrow: cc.Node = null;

    @property
    isArrowOpen: number = 1;

    @property({ type: cc.Enum(PlayerStateID) })
    type: PlayerStateID = PlayerStateID.TOLET;

    protected model:StateItemModel;

    start() {
        this.model = Player.instance().getStateItemModel(this.type)
        if (this.isArrowOpen) {
            this.setArrowVisible(true)
        } else {
            this.setArrowVisible(false)
        }

        GlobalEvent.instance().on(EventName.UPDATE_PLAYER_STATE, this.updatePlayerState, this)
        this.updatePlayerState(this.type)

    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.UPDATE_PLAYER_STATE, this.updatePlayerState, this)
    }

    updatePlayerState(type: PlayerStateID) {
        // cc.log("updatePlayerState type  ", type, ' this.type ', this.type)
        if (this.type == type) {
            this.setPercent(this.model.getPercent())
        }

    }

    setArrowVisible(f: boolean) {
        if (this.arrow) {
            this.arrow.active = f;
        }

    }

    setPercent(p: number) {
        this.mask.node.height = 128 * p;
    }

    // update (dt) {}
}
